//
//  MainTabBar.swift
//  WeiBoSwift
//
//  Created by zc on 16/6/17.
//  Copyright © 2016年 zc. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    //默认的构造方法
   override init(frame: CGRect) {
        super.init(frame: frame)
    setupUI()
    }
    //如果重写init（frame：）系统会默认这个空间是通过代码创建的
    required init?(coder aDecoder: NSCoder) {
        //只有默认报错的语句   如果有调用者通过storyboard / xib 创建了该对象 这个时候程序就会崩溃
        super.init(coder: aDecoder)
        
    }
    
    private func setupUI(){
        //添加中间按钮
        addSubview(composeBtn)
       // print("--------\(self.composeBtn)")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //手动修改所有的按钮的位置
        //遍历所有的子视图
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        //定义递增变量
        var index:CGFloat = 0
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
               // print(subView)
                subView.frame = CGRectOffset(rect, w * index, 0)
//                if index == 1 {
//                    index++
//                }
//                index++
                
                //三目
                index += index == 1 ? 2 :1
            }
        }
        //设置中间按钮的位置
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
        bringSubviewToFront(composeBtn)
        
    }
    
    //懒加载
    
    lazy var composeBtn: UIButton = {
        let btn = UIButton()
        //设置按钮
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
         btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        return btn
    }()

}
